<?php
	header('content-type:text/html;charset=utf-8'); 
	//重要!兩種情況中都要判斷DID是否存在,DID不存在,還要判斷工單號和機台號是否為空
	//2018-3-16 11:08:32注釋:想起來好像前端就應該做判斷過濾掉這種情況
	//$GX=$_GET["nos"];
	
	
	$Times = $_POST["Times"];//时间
	
	$conn=mysql_connect("127.0.0.1:3306","root","tdled2018");
	if (!$conn){
		die('Could not connect: ' . mysql_error());
	}
	
	mysql_select_db("tddb", $conn);
	
	mysql_query("set character set 'utf8'");//读库 
	
	mysql_query("set names 'utf8'");//写库 
	
	$sql1="SELECT			
	COUNT(		
		CASE WHEN	
			A.GX = '固晶'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END	
	) AS 固晶操机人数
	,COUNT(		
		CASE WHEN	
			A.GX = '固烤'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END	
	) AS 固烤操机人数	
	,COUNT(		
		CASE WHEN	
			A.GX = '焊线'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END	
	) AS 焊线操机人数
	,COUNT(		
		CASE WHEN	
			A.GX = '焊洗'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END
     ) AS 焊洗操机人数
	,COUNT(		
		CASE WHEN	
			A.GX = '点胶'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END	
	) AS 点胶操机人数
	,COUNT(		
		CASE WHEN	
			A.GX = '点烤'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END	
	) AS 点烤操机人数		
	,COUNT(		
		CASE WHEN	
			A.GX = '切割'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END	
	) AS 切割操机人数		
	,COUNT(		
		CASE WHEN	
			A.GX = '分光'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END	
	) AS 分光操机人数		
	,COUNT(		
		CASE WHEN	
			A.GX = '编带'
		THEN 	
			A.CZY
		ELSE	
			NULL
		END	
	) AS 编带操机人数		
FROM			
	(		
		SELECT DISTINCT	
			 GongXu AS GX
			,ChanPinLeiXing AS LX
			,(CaoZuoYuan) AS CZY
		FROM	
			 d_d_temp
			,d_h_temp
		WHERE	
			RiQi ='".$Times."'
		AND	
			d_d_temp.HID = d_h_temp.HID
		AND	
			ChanPinLeiXing = 'EMC'
	) AS A		
union SELECT			
	COUNT(		
		CASE WHEN	
			A.GX = '固晶'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 固晶开机数
	,COUNT(		
		CASE WHEN	
			A.GX = '固烤'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 固烤开机数		
	,COUNT(		
		CASE WHEN	
			A.GX = '焊线'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 焊线开机数
	,COUNT(		
		CASE WHEN	
			A.GX = '焊洗'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 焊洗开机数		
	,COUNT(		
		CASE WHEN	
			A.GX = '点胶'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 点胶开机数
	,COUNT(		
		CASE WHEN	
			A.GX = '点烤'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 点烤开机数		
	,COUNT(		
		CASE WHEN	
			A.GX = '切割'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 切割开机数		
	,COUNT(		
		CASE WHEN	
			A.GX = '分光'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 分光开机数		
	,COUNT(		
		CASE WHEN	
			A.GX = '编带'
		THEN 	
			A.JT
		ELSE	
			NULL
		END	
	) AS 编带开机数		
FROM			
	(		
		SELECT DISTINCT	
			 GongXu AS GX
			,ChanPinLeiXing AS LX
			,(JiTai) AS JT
		FROM	
			 d_d_temp
			,d_h_temp
		WHERE	
			RiQi ='".$Times."'
		AND	
			d_d_temp.HID = d_h_temp.HID
		AND	
			ChanPinLeiXing = 'EMC'
	) AS A";
	
	
	
	$result = mysql_query($sql1); 
	
	$dbdata = array();
	$dbdatas = array();
	$category = array();
	//$category = {0: "操机员数", 1: "机台开机数", 2: "机台待机数", 3: "机台故障/待验数", 4: "WIP(K)產品系列|日標配", 5: "0"};
	/* $category[0] =("10"=>"操机员数"); 
	$category[1] =("10"=>"机台开机数"); 
	$category[2] =("10"=>"机台待机数"); 
	$category[3] =("10"=>"机台故障/待验数"); 
	$category[4] =("10"=>"WIP(K)產品系列|日標配");    */
	$category[0]=$a1=array("10"=>"操机员数");
	$category[1]=$a1=array("10"=>"机台开机数"); 
	$category[2]=$a1=array("10"=>"机台待机数"); 
	
	$category[3]=$a1=array("10"=>"机台故障/待验数"); 
	$category[4]=$a1=array("10"=>"WIP(K)產品系列|日標配");
	//Fetch into associative array
	$i=0;
	while ( $row = mysql_fetch_array($result))  {
		//echo json_encode($row)."</br>";
		//$row=$row.'操机员数';
		$dbdata[]= array_merge($category[$i],$row);
		//$dbdata[]= $row;
		//printf($dbdata);
		//echo $dbdata[];
		$i++;
	}
	
	//Print array in JSON format
	//array.splice(2, 0, "three");
	echo json_encode($dbdata);
	
	
?>